package com.jinhongone.beans.factory.xml;

import com.jinhongone.beans.factory.support.BeanDefinitionRegistry;
import com.jinhongone.beans.PropertyValue;
import com.jinhongone.beans.factory.support.SimgleBeanDefinitionRegistry;
import com.jinhongone.beans.factory.config.BeanDefinition;
import com.jinhongone.beans.factory.support.BeanDefinitionReader;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

/**
 * @author jinho
 * @className XmlBeanDefinitionReader
 * @description beanDefinitinReader接口针对xml文件的具体实现
 * @date 2025/4/25 16:40
 */
public class XmlBeanDefinitionReader implements BeanDefinitionReader {

    private BeanDefinitionRegistry registry;

    public XmlBeanDefinitionReader() {
        //初始化bean注册表
        this.registry = new SimgleBeanDefinitionRegistry();
    }

    @Override
    public BeanDefinitionRegistry getRegistry() {
        return registry;
    }

    @Override
    public void loadBeanDefinitions(String configLocation) throws Exception {
        /*
         * 代码思路：
         *   1.根据xml地址获取xml的inputstream
         *   2.使用dom4j对xml文件的inputstream进行解析
         *   3.解析xml中bean标签、property标签
         *   4.将解析后的beanDefinition对象注册到注册表中
         * */
        // 1.根据xml地址获取xml的inputstream-通过类加载获取资源的stream
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(configLocation);
        // 2.使用dom4j对xml文件的inputstream进行解析
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(resourceAsStream);
        Element rootElement = document.getRootElement(); //获取xml中的跟标签<beans>
        // 3.解析xml中bean标签、property标签
        parseElement(rootElement);

    }

    private void parseElement(Element rootElement) {
        //获取root标签中所有的子标签（bean标签）
        List<Element> elements = rootElement.elements();
        for (Element element : elements) {
            //获取bean标签中各属性值
            String id = element.attributeValue("id");
            String className = element.attributeValue("class");
            //将xml中bean标签封装为beanDefinition
            BeanDefinition bean = new BeanDefinition();
            bean.setId(id);
            bean.setClassName(className);
            //获取bean标签中property标签
            List<Element> propertyElements = element.elements();
            if (!propertyElements.isEmpty()) {
                for (Element propertyElement : propertyElements) {
                    String name = propertyElement.attributeValue("name");
                    String ref = propertyElement.attributeValue("ref");
                    String value = propertyElement.attributeValue("value");
                    PropertyValue propertyValue = new PropertyValue(name, ref, value);
                    bean.getMutablePropertyValue().addPropertyValue(propertyValue);
                }
            }
            // 4.将解析后的beanDefinition对象注册到注册表中
            registry.registryBeanDefinition(id, bean);
        }
    }
}
